package com.ffms.dao.impl;

import com.ffms.dao.AdminDao;
import com.ffms.entity.Admin;
import com.ffms.util.JDBCUtils;

import java.sql.*;


/**
 * @author 从卿
 * @create 2022/3/3-20:22
 */
public class AdminDaoImpl implements AdminDao {

    @Override
//    管理员登录
    public Admin findAdmin(String username, String password) {
        Admin admin1 = null;
        Admin admin = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from tables.t_admin where username = '" + username + "' and password = '" + password + "' ";
            //3.获取执行sql的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {

                admin1 = new Admin(username);

                admin = new Admin(username, password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                stmt.close();
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return admin;


    }

    //判断用户名是否正确
    public Admin findAdminByName(String username) {

        Admin admin = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from tables.t_admin where username = '" + username + "' ";
            //3.获取执行sql的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {


                admin = new Admin(username);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                stmt.close();
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return admin;
    }

    public Admin findAdminBypwd(String password) {
        Admin admin = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from tables.t_admin where password = '" + password + "' ";
            //3.获取执行sql的对象
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                admin = new Admin(password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
                stmt.close();
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return admin;
    }

    @Override
    public int update(Admin ad) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        int n = 0;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "update t_admin set password=? where username=? ";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);
            Object[] prams = {ad.getPassword(), ad.getUserName()};
            for (int i = 0; i < prams.length; i++) {
                pstmt.setObject(i + 1, prams[i]);
            }
            n = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return n;

    }


    @Override
//    管理员用户添加
    public int save(Admin admin) {

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int n = 0;
        try {
            conn = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "insert into tables.t_admin values (?,?)";
            //3.获取执行sql的对象
            pstmt = conn.prepareStatement(sql);

            Object[] prams = {admin.getUserName(), admin.getPassword()};
            for (int i = 0; i < prams.length; i++) {
                pstmt.setObject(i + 1, prams[i]);

            }
            n = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;

    }
}
